<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;

use Exception;

class OrderItem extends BaseModel
{
    public $timestamps = false;
    public static function AddItem($orderObject,$input)
    {
        $carts_id = json_decode($input['cart_id'],true);
        $carts = Cart::whereIn('cart_items.id',$carts_id)
            ->join('products_sku','cart_items.product_sku_id', '=', 'products_sku.id')
            ->get()
            ->toArray();
        $amount = 0;
        foreach ($carts as $key=>$val) {
            $amount += $val['amount'] * ProductSku::getPriceById($val['product_sku_id']);
        }
        $orderObject->total_amount = $amount + $orderObject['express_fee'];
        if (!$orderObject->save()) {
            throw new Exception('订单保存失败1');
        }
        foreach ($carts as $val) {
            $orderItem = new self();
            $orderItem->order_id = $orderObject->id;
            $orderItem->product_id = $val['product_id'];
            $orderItem->product_sku_id = $val['product_sku_id'];
            $sku = ProductSku::getSkuById($val['product_sku_id']);
            if ($sku) {
                $orderItem->sku_name = $sku['name'];
            }
            $orderItem->spec_item = ProductSpec::getSpecItem($val['product_id']);
            $orderItem->amount = $val['amount'];
            $price = ProductSku::getPriceById($val['product_sku_id']);
            $orderItem->price = $price;
            $orderItem->save();
            $product = Product::getOne($val['product_id']);
            if ($product) {
                $orderObject->product_name = $product['title'];
                $orderObject->product_num = $val['amount'];
                $orderObject->product_price = $price;
                $product_price_total = $price*$val['amount'];
                $orderObject->product_price_total = $product_price_total;
                $fee = sprintf("%.2f",$price*$val['amount']*0.006);
                $orderObject->actual_money = $product_price_total-$fee;
                if ($sku) {
                    $orderObject->sku_name = $sku['name'];
                }
                $orderObject->save();
            }
        }
        //删除购物车
        foreach ($carts_id as $val) {
            $cart = Cart::find($val);
            if ($cart) {
                $cart->delete();
            }
        }
    }

    public static function getOrderItemByOrderId($order_id)
    {

        $order_item = OrderItem::where('order_id', $order_id)
            ->select('order_items.*','title','image')
            ->join('products','order_items.product_id', '=', 'products.id')
            ->get()
            ->toArray();
        foreach ($order_item as &$val) {
            $val['image'] = self::jsonDecodeImg($val['image']);
            $val['sku'] = ProductSku::getSkuById($val['product_sku_id']);
        }
        return $order_item;
    }

    public static function getItemById($id)
    {
        $order_item = OrderItem::where('order_items.id',$id)
            ->select('order_items.*','title','image')
            ->join('products','order_items.product_id', '=', 'products.id')
            ->get()
            ->toArray();
        foreach ($order_item as &$val) {
            $val['image'] = self::jsonDecodeImg($val['image']);
        }
        return $order_item;
    }

    public static function getCommit($product_id)
    {
        $where = [
            ['product_id', '=', $product_id],
            ['is_review', '=', 1],
        ];
        $item = self::where($where)
            ->select('id','order_id','rating','is_review','review','replay','reviewed_at')
            ->paginate(10)
            ->orderBy('id', 'desc')
            ->toArray();
        foreach($item['data'] as $key=>&$val){
            $order = Order::find($val['order_id']);
            if ($order) {
                $user = User::find($order['user_id']);
                if ($user) {
                    $val['user'] = $user->toArray();
                }
            }
        }
        return $item;
    }

    //订单支付成功库存操作
    public static function orderStock($order_id)
    {
        $item = OrderItem::where('order_id', $order_id)->get();

        foreach ($item as $val) {
            $prodcutSku = ProductSku::find($val['product_sku_id']);
            if ($prodcutSku) {
                $prodcutSku->decrement('stock', $val['amount']);    //减库存
            }
            $product = Product::find($val['product_id']);
            if ($product) {
                $product->increment('sold_count',$val['amount']);  //加销量
            }
        }
    }
}